<?php


namespace coco\model;


use coco\Model;

class Relation
{
    /**@var Model $model */
    private $model;
    private $fk;
    private $pk;
    private $type;
    private $on;
    private $join_type;
    private $_field = '';

    public function __construct(Model $parent, Model $model, string $fk, string $pk, string $type)
    {
        $this->model = $model;
        if (empty($fk)) {
            if ($type === 'belongsTo') {
                $fk = $this->model->table . '_id';
            } else {
                $fk = $parent->table . '_id';
            }
        }
        $this->fk = $fk;
        $this->pk = $pk;
        $this->type = $type;//belongsTo hasOne hasMany
        $this->join_type = 'LEFT';
        if ($type === 'belongsTo') {
            $this->on = $parent->alias . '.' . $fk . '=' . $this->model->alias . '.' . $pk;
        } else {
            $this->on = $parent->alias . '.' . $pk . '=' . $this->model->alias . '.' . $fk;
        }
    }

    public function field(string $field)
    {
        $this->_field = $field;
        return $this;
    }

    //shezhi

    /**
     * @return string
     */
    public function getType(): string
    {
        return $this->type;
    }

    /**
     * @return Model
     */
    public function getModel(): Model
    {
        return $this->model;
    }

    /**
     * @return string
     */
    public function getOn(): string
    {
        return $this->on;
    }

    /**
     * @return string
     */
    public function getJoinType(): string
    {
        return $this->join_type;
    }

    /**
     * @return string
     */
    public function getField(): string
    {
        return $this->_field;
    }

}